<html>
<body>

<?php include 'mainheader.php'; ?>
<?php include 'databaseutil.php'; ?>


<?php

function swapValues( &$a, &$b )
{
	$temp = $a;
	$a = $b;
	$b = $temp;
}

function orderByPercent( &$perc, &$asset, &$firstDate, &$lastDate )
{
	for( $i = 0; $i < 12; $i = $i + 1 )
	{
		for( $j = $i + 1; $j < 12; $j = $j + 1 )
		{
			$curPerc = $perc[$i];
			if( $perc[$j] > $curPerc )
			{
				// swap
				swapValues( $perc[$j], $perc[$i] );
				swapValues( $asset[$j], $asset[$i] );
				swapValues( $firstDate[$j], $firstDate[$i] );
				swapValues( $lastDate[$j], $lastDate[$i] );
			}
		}
	} 
}


// Main

if( !isset($_GET["fromMonth"]) || !isset($_GET["fromYear"]) || !isset($_GET["toMonth"]) || !isset($_GET["toYear"]) )
{
	echo "<br><br><br>";
	echo "<form name='input' action='ranking.php' method='get'>";
	echo "From month: ";
	echo "<select name='fromMonth'>";
		echo "<option value='1'>January</option>";
		echo "<option value='2'>February</option>";
		echo "<option value='3'>March</option>";
		echo "<option value='4'>April</option>";
		echo "<option value='5'>May</option>";
		echo "<option value='6'>June</option>";
		echo "<option value='7'>July</option>";
		echo "<option value='8'>August</option>";
		echo "<option value='9'>September</option>";
		echo "<option value='10'>October</option>";
		echo "<option value='11'>November</option>";
		echo "<option value='12'>December</option>";
	echo "</select>";
	echo "   From Year: <input type='text' name='fromYear'><br>";
	echo "<br>";
	echo "To month: ";
	echo "<select name='toMonth'>";
		echo "<option value='1'>January</option>";
		echo "<option value='2'>February</option>";
		echo "<option value='3'>March</option>";
		echo "<option value='4'>April</option>";
		echo "<option value='5'>May</option>";
		echo "<option value='6'>June</option>";
		echo "<option value='7'>July</option>";
		echo "<option value='8'>August</option>";
		echo "<option value='9'>September</option>";
		echo "<option value='10'>October</option>";
		echo "<option value='11'>November</option>";
		echo "<option value='12'>December</option>";
	echo "</select>";
	echo "   To Year: <input type='text' name='toYear'><br><br>";
	echo "<input type='submit' value='Submit'>";
	echo "</form>";
	return;
}

$assetsArray = array( "2Qtos C. Grande", "2Qtos Curicica", "GOLD", "SILVER", "DOW", "iBovespa", "PETR4", "VALE5", "OGXP3", "USIM5", "Dollar", "Euro" );
$monthArray = array( 1 => "January", 2 => "February", 3 => "March", 4 => "April", 5 => "May", 6 => "June", 7 => "July", 8 => "August", 9 => "September", 10 => "October", 11 => "November", 12 => "December");

// Get values from form
$fromMonth = $_GET["fromMonth"];
$fromYear = $_GET["fromYear"];
$toMonth = $_GET["toMonth"];
$toYear = $_GET["toYear"];

// Get asset values from earlier month/year
$fromValues = array();
$fromValues[0] = getAssetValue( "2Qtos C. Grande", $fromMonth, $fromYear );
$fromValues[1] = getAssetValue( "2Qtos Curicica", $fromMonth, $fromYear );
$fromValues[2] = getAssetValue( "GOLD", $fromMonth, $fromYear );
$fromValues[3] = getAssetValue( "SILVER", $fromMonth, $fromYear );
$fromValues[4] = getAssetValue( "DOW", $fromMonth, $fromYear );
$fromValues[5] = getAssetValue( "iBovespa", $fromMonth, $fromYear );
$fromValues[6] = getAssetValue( "PETR4", $fromMonth, $fromYear );
$fromValues[7] = getAssetValue( "VALE5", $fromMonth, $fromYear );
$fromValues[8] = getAssetValue( "OGXP3", $fromMonth, $fromYear );
$fromValues[9] = getAssetValue( "USIM5", $fromMonth, $fromYear );
$fromValues[10] = getAssetValue( "Dollar", $fromMonth, $fromYear );
$fromValues[11] = getAssetValue( "Euro", $fromMonth, $fromYear );

// Valida as informacoes do banco para evitar divisao por zero
if( $fromValues[0] == NULL || $fromValues[1] == NULL || $fromValues[2] == NULL || $fromValues[3] == NULL || $fromValues[4] == NULL || $fromValues[5] == NULL || $fromValues[6] == NULL || $fromValues[7] == NULL || $fromValues[8] == NULL || $fromValues[9] == NULL || $fromValues[10] == NULL || $fromValues[11] == NULL )
{
	echo "<br><br>";
	echo "<h4>Sorry, earlier month/year doest not exist in database.</h4>";
	echo "<a href = 'ranking.php'>Back</a>";
	return;
}

if( $fromValues[0] == 0 || $fromValues[1] == 0 || $fromValues[2] == 0 || $fromValues[3] == 0 || $fromValues[4] == 0 || $fromValues[5] == 0 || $fromValues[6] == 0 || $fromValues[7] == 0 || $fromValues[8] == 0 || $fromValues[9] == 0 || $fromValues[10] == 0 || $fromValues[11] == 0 )
{
	echo "<br><br>";
	echo "<h4>Error. At least one of these assets is about to evoke a division by zero error. Check it out!!</h4>";
	echo "<h5>2Qtos Campo Grande: " . $cg . "</h5>";
	echo "<h5>2Qtos Curicica: " . $cu . "</h5>";
	echo "<h5>Gold: " . $g . "</h5>";
	echo "<h5>Silver: : " . $s . "</h5>";
	echo "<h5>Dow: " . $do . "</h5>";
	echo "<h5>iBovespa: " . $b . "</h5>";
	echo "<h5>PETR4: " . $p . "</h5>";
	echo "<h5>VALE5: " . $v . "</h5>";
	echo "<h5>OGXP3: " . $o . "</h5>";
	echo "<h5>USIM5: " . $u . "</h5>";
	echo "<h5>Dollar: " . $dr . "</h5>";
	echo "<h5>Euro: " . $er . "</h5>";
	echo "<a href = 'ranking.php'>Back</a>";
	return;
}

// Get asset values from later month/year
$toValues = array();
$toValues[0] = getAssetValue( "2Qtos C. Grande", $toMonth, $toYear );
$toValues[1] = getAssetValue( "2Qtos Curicica", $toMonth, $toYear );
$toValues[2] = getAssetValue( "GOLD", $toMonth, $toYear );
$toValues[3] = getAssetValue( "SILVER", $toMonth, $toYear );
$toValues[4] = getAssetValue( "DOW", $toMonth, $toYear );
$toValues[5] = getAssetValue( "iBovespa", $toMonth, $toYear );
$toValues[6] = getAssetValue( "PETR4", $toMonth, $toYear );
$toValues[7] = getAssetValue( "VALE5", $toMonth, $toYear );
$toValues[8] = getAssetValue( "OGXP3", $toMonth, $toYear );
$toValues[9] = getAssetValue( "USIM5", $toMonth, $toYear );
$toValues[10] = getAssetValue( "Dollar", $toMonth, $toYear );
$toValues[11] = getAssetValue( "Euro", $toMonth, $toYear );

// Valida as informacoes do banco para evitar divisao por zero
if( $toValues[0] == NULL || $toValues[1] == NULL || $toValues[2] == NULL || $toValues[3] == NULL || $toValues[4] == NULL || $toValues[5] == NULL || $toValues[6] == NULL || $toValues[7] == NULL || $toValues[8] == NULL || $toValues[9] == NULL || $toValues[10] == NULL || $toValues[11] == NULL )
{
	echo "<br><br>";
	echo "<h4>Sorry, later month/year doest not exist in database.</h4>";
	echo "<a href = 'ranking.php'>Back</a>";
	return;
}

if( $toValues[0] == 0 || $toValues[1] == 0 || $toValues[2] == 0 || $toValues[3] == 0 || $toValues[4] == 0 || $toValues[5] == 0 || $toValues[6] == 0 || $toValues[7] == 0 || $toValues[8] == 0 || $toValues[9] == 0 || $toValues[10] == 0 || $toValues[11] == 0 )
{
	echo "<br><br>";
	echo "<h4>Error. At least one of these assets is about to evoke a division by zero error. Check it out!!</h4>";
	echo "<h5>2Qtos Campo Grande: " . $cg . "</h5>";
	echo "<h5>2Qtos Curicica: " . $cu . "</h5>";
	echo "<h5>Gold: " . $g . "</h5>";
	echo "<h5>Silver: : " . $s . "</h5>";
	echo "<h5>Dow: " . $do . "</h5>";
	echo "<h5>iBovespa: " . $b . "</h5>";
	echo "<h5>PETR4: " . $p . "</h5>";
	echo "<h5>VALE5: " . $v . "</h5>";
	echo "<h5>OGXP3: " . $o . "</h5>";
	echo "<h5>USIM5: " . $u . "</h5>";
	echo "<h5>Dollar: " . $dr . "</h5>";
	echo "<h5>Euro: " . $er . "</h5>";
	echo "<a href = 'ranking.php'>Back</a>";
	return;
}

// Calcula o delta entre as valorizacoes
$percentValues = array();
$percentValues[0] = ($toValues[0] - $fromValues[0]) * 100 / $fromValues[0];
$percentValues[1] = ($toValues[1] - $fromValues[1]) * 100 / $fromValues[1];
$percentValues[2] = ($toValues[2] - $fromValues[2]) * 100 / $fromValues[2];
$percentValues[3] = ($toValues[3] - $fromValues[3]) * 100 / $fromValues[3];
$percentValues[4] = ($toValues[4] - $fromValues[4]) * 100 / $fromValues[4];
$percentValues[5] = ($toValues[5] - $fromValues[5]) * 100 / $fromValues[5];
$percentValues[6] = ($toValues[6] - $fromValues[6]) * 100 / $fromValues[6];
$percentValues[7] = ($toValues[7] - $fromValues[7]) * 100 / $fromValues[7];
$percentValues[8] = ($toValues[8] - $fromValues[8]) * 100 / $fromValues[8];
$percentValues[9] = ($toValues[9] - $fromValues[9]) * 100 / $fromValues[9];
$percentValues[10] = ($toValues[10] - $fromValues[10]) * 100 / $fromValues[10];
$percentValues[11] = ($toValues[11] - $fromValues[11]) * 100 / $fromValues[11];


// Ordena a tabela
orderByPercent( $percentValues, $assetsArray, $fromValues, $toValues );

// Compoe a tabela com a valorizacao de cada ativo
echo "<br><br>";
echo "<table border='1' width='40%'>";
echo "<col width='20'>";
echo "<col width='10'>";
echo "<col width='10'>";
echo "<col width='10'>";
echo "<tr>";
echo "<th>Assets</th><th>" . $monthArray[$fromMonth] . "/" . $fromYear . "</th><th>" . $monthArray[$toMonth] . "/". $toYear . "</th><th>Percent</th>";
echo "</tr>";
echo "<tr>";
echo "<th>" . $assetsArray[0] . "</th><td align='center'>" . number_format($fromValues[0],2) . "</td><td align='center'>" . number_format($toValues[0], 2) . "</td><td align='center'>" . number_format($percentValues[0], 2) . "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" . $assetsArray[1] . "</th><td align='center'>" . number_format($fromValues[1], 2) . "</td><td align='center'>" . number_format($toValues[1], 2) . "</td><td align='center'>" . number_format($percentValues[1], 2) . "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" . $assetsArray[2] . "</th><td align='center'>" . number_format($fromValues[2], 2) . "</td><td align='center'>" . number_format($toValues[2], 2) . "</td><td align='center'>" . number_format($percentValues[2], 2) . "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" . $assetsArray[3] . "</th><td align='center'>" . number_format($fromValues[3], 2) . "</td><td align='center'>" . number_format($toValues[3], 2) . "</td><td align='center'>" . number_format($percentValues[3], 2) . "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" . $assetsArray[4] . "</th><td align='center'>" . number_format($fromValues[4], 2) . "</td><td align='center'>" . number_format($toValues[4], 2) . "</td><td align='center'>" . number_format($percentValues[4], 2) . "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" . $assetsArray[5] . "</th><td align='center'>" . number_format($fromValues[5], 2) . "</td><td align='center'>" . number_format($toValues[5], 2) . "</td><td align='center'>" . number_format($percentValues[5], 2) . "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" . $assetsArray[6] . "</th><td align='center'>" . number_format($fromValues[6], 2) . "</td><td align='center'>" . number_format($toValues[6], 2) . "</td><td align='center'>" . number_format($percentValues[6], 2) . "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" . $assetsArray[7] . "</th><td align='center'>" . number_format($fromValues[7], 2) . "</td><td align='center'>" . number_format($toValues[7], 2) . "</td><td align='center'>" . number_format($percentValues[7], 2) . "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" . $assetsArray[8] . "</th><td align='center'>" . number_format($fromValues[8], 2) . "</td><td align='center'>" . number_format($toValues[8], 2) . "</td><td align='center'>" . number_format($percentValues[8], 2) . "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" . $assetsArray[9] . "</th><td align='center'>" . number_format($fromValues[9], 2) . "</td><td align='center'>" . number_format($toValues[9], 2) . "</td><td align='center'>" . number_format($percentValues[9], 2) . "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" . $assetsArray[10] . "</th><td align='center'>" . number_format($fromValues[10], 2) . "</td><td align='center'>" . number_format($toValues[10], 2) . "</td><td align='center'>" . number_format($percentValues[10], 2) . "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" . $assetsArray[11] . "</th><td align='center'>" . number_format($fromValues[11], 2) . "</td><td align='center'>" . number_format($toValues[11], 2) . "</td><td align='center'>" . number_format($percentValues[11], 2) . "</td>";
echo "</tr>";
echo "</table>";

?>

</body>
</html>
